package tallinn;

import processing.core.PApplet;
import simpleGeom.Bounds3D;
import simpleGeom.Mesh;
import simpleGeom.MeshNode;
import simpleGui.PCamera3D;

public class Tallinn extends PApplet {
	Mesh mesh;
	float a=3.2f;
	float b=2.6f;
	float c=5.76f;
	float[] rasterY=new float[]{0,c,c};
	float[]rasterX=new float[]{0,a,a,b,b,b,a,a,b,b,b,b,b,b,b,b,a,a,b,b,b,b,b,b,b,b,a,a,b,b,b,b,b,b,b,b,a,a,b,b,b,a,a};
	MeshNode[][] grid=new MeshNode[rasterX.length][rasterY.length];
	int[][] types;
	PCamera3D pcam3D;

	public void setup() {
		size(800, 600, P3D);
		pcam3D = new PCamera3D(this);

		mesh = new Mesh();
		float cX = 0;
		float cY = 0;
		for (int x = 0; x < rasterX.length; x++) {
			cY = 0;
			cX += rasterX[x];
			for (int y = 0; y < rasterY.length; y++) {
				cY += rasterY[y];
				MeshNode node = new MeshNode(cX, cY);
				mesh.addNode(node);
				grid[x][y] = node;

			}
		}
		mesh.rebuildIds();
		
		for (int z = 0; z < 5; z++) {
			cX = 0;
			for (int x = 0; x < rasterX.length - 1; x++) {
				cX += rasterX[x];
				cY = 0;
				for (int y = 0; y < rasterY.length - 1; y++) {
					cY += rasterY[y];
					MeshNode n1 = new MeshNode(cX, cY, z * 3);
					MeshNode n2 = new MeshNode(cX + rasterX[x + 1], cY, z * 3);
					MeshNode n3 = new MeshNode(cX + rasterX[x + 1], cY
							+ rasterY[y + 1], z * 3);
					MeshNode n4 = new MeshNode(cX, cY + rasterY[y + 1], z * 3);
					
					MeshNode n5 = new MeshNode(cX, cY, (z+1) * 3);
					MeshNode n6 = new MeshNode(cX + rasterX[x + 1], cY, (z+1) * 3);
					MeshNode n7 = new MeshNode(cX + rasterX[x + 1], cY
							+ rasterY[y + 1], (z+1) * 3);
					MeshNode n8 = new MeshNode(cX, cY + rasterY[y + 1], (z+1) * 3);
					
					// building.addQuad(n1.getId(), n2.getId(), n3.getId(),
					// n4.getId());
					mesh.addQuad(n1, n2, n3, n4);
					mesh.addQuad(n2, n3, n7, n6);
					mesh.addQuad(n3, n4, n8, n7);
					//building.addQuad(n3, n4, n8, n7);
				}
			}
		}
	}

public void draw(){
	background(255);
	lights();
	Bounds3D b3d=mesh.getBounds();
	pcam3D.apply(g);
	
	scale(0.05f);
	translate(-b3d.getCenterX(),-b3d.getCenterY(),-b3d.getCenterZ());
	mesh.display3D(g);
}

}
